package
{
	import com.greensock.TweenLite;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	
	import utils.ClusterHelper;
	
	public class Cluster extends Sprite
	{
		private var _positionArr:Array
		private var positionArr1:Array= [
			[0,0,0,0],
			[1,1,1,0],
			[0,1,0,0],
			[0,0,0,0]]
		private var positionArr2:Array= [
			[0,1,0,0],
			[1,1,0,0],
			[0,1,0,0],
			[0,0,0,0]]
		private var positionArr3:Array= [
			[0,1,0,0],
			[1,1,1,0],
			[0,0,0,0],
			[0,0,0,0]]
		private var positionArr4:Array= [
			[0,1,0,0],
			[0,1,1,0],
			[0,1,0,0],
			[0,0,0,0]]
		/**
		 *存储形状的数组 
		 */		
		private var positionsArr:Array = [positionArr1,positionArr2,positionArr3,positionArr4];
		/**
		 *形状索引 
		 */		
		private var positionIndex:int = 0;
		
		public function get positionArr():Array
		{
			return _positionArr;
		}
		
		public function set positionArr(value:Array):void
		{
			_positionArr = value;
			
			clusterHelper = new ClusterHelper(_positionArr);
			
			var g:Graphics = this.graphics;
			var color:int = 0xff0000;
			//画之前清空之前画的形状
			g.clear();
			for(var i:int = 0;i<positionArr.length;i++)
			{
				var arr:Array = positionArr[i];
				for(var j:int = 0;j<arr.length;j++)
				{
					if(arr[j] == 1)
					{
						g.lineStyle(2);
						g.beginFill(color);
						g.drawRect(size *j,size *i,size,size);
						g.endFill();	
					}
					//画透明块
					g.lineStyle(1);
					g.beginFill(0x00ff00,0.3);
					g.drawRect(size *j,size *i,size,size);
					g.endFill();	
				}
			}
		}
		
		;
		private var size:int = 24;
		
		
		public function Cluster()
		{
			super();
			positionArr = positionsArr[positionIndex]
		}
		/**
		 *行索引 
		 */		
		public var rowIndex:int = 0;
		/**
		 *列索引 
		 */		
		public var columIndex:int = 0;
		
		private var clusterHelper:ClusterHelper;
		
		public function get top():int
		{
			return clusterHelper.top;
		}
		public function get bottom():int
		{
			return clusterHelper.bottom;
		}
		public function get left():int
		{
			return clusterHelper.left;
		}
		public function get right():int
		{
			return clusterHelper.right;
		}
		
		/**
		 *下落 
		 * 
		 */		
		public function down():void
		{
			if(rowIndex+bottom+1<TerisDemo.rowNum)
			{
				rowIndex++;
				updatePosition();
			}
		}
		/**
		 *上移 
		 * 
		 */		
		public function up():void
		{
			if(rowIndex+top>0)
			{
				rowIndex--;
				updatePosition();
			}
		}
		
		/**
		 *左移 
		 * 
		 */		
		public function moveRight():void
		{
			if(columIndex+right+1<TerisDemo.columNum)
			{
				columIndex++;
				updatePosition();
			}
		}
		
		public function moveLeft():void
		{
			if(columIndex+left>0)
			{
				columIndex--;
				updatePosition();
			}
		}
		
		public function updatePosition():void
		{
			var _x:Number = columIndex *size;
			var _y:Number = rowIndex * size;
			
			x = _x;
			y = _y;
		}
		
		public function rotate():void
		{
			var tempIndex:int;
			if(positionIndex<positionsArr.length-1)
			{
				tempIndex = positionIndex+1;
			}else
			{
				tempIndex = 0;
			}
			//
			var canRotate:Boolean = judgeRotate(positionsArr[tempIndex]);
			if(canRotate)
			{
				positionIndex = tempIndex
				positionArr = positionsArr[positionIndex]
			}
		}
		/**
		 *判断是否可以旋转 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function judgeRotate(arr:Array):Boolean
		{
			// TODO Auto Generated method stub
			var clusterHelper:ClusterHelper = new ClusterHelper(arr)
			if(columIndex+clusterHelper.left < 0)
			{
				return false;
			}else if(columIndex+clusterHelper.right +1 >TerisDemo.columNum)
			{
				return false;
			}
			else if(rowIndex+clusterHelper.top <0)
			{
				return false;
			}else if(rowIndex+clusterHelper.bottom +1 >TerisDemo.rowNum)
			{
				return false;
			}
			
			return true;
		}
	}
}